﻿using PLC.Interface.BaseMenus;
using PLC.Interface.BaseUserPermissions;
using PLC.Interface.BaseUsers;
using PLCWpfTest.Common;
using PLCWpfTest.Common.Extensions;
using PLCWpfTest.Models;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using YWM.Common.Exceptions;
using YWM.Dto;

namespace PLCWpfTest.ViewModels
{
    public class UserOperationViewModel : OperationViewModel
    {
        public NameValueModel<byte>[] Sexs { get; set; }

        public NameValueModel<bool>[] IsActives { get; set; }

        private readonly IBaseUserAppService _baseUserAppService;

        private readonly IBaseMenuAppService _baseMenuAppService;

        public ObservableCollection<NameValueSelectedModel<string>> Menus { get; set; } = new ObservableCollection<NameValueSelectedModel<string>>();

        public BaseUserDto BaseUser { get; set; } = new BaseUserDto();

        public UserOperationViewModel(IBaseUserAppService baseUserAppService, IBaseMenuAppService baseMenuAppService, BaseUserDto baseUser)
        {
            Sexs = new NameValueModel<byte>[] {
                new NameValueModel<byte>(Lang["男"], 0),
                new NameValueModel<byte>(Lang["女"], 1)
            };
            IsActives = new NameValueModel<bool>[] {
                new NameValueModel<bool>(Lang["是"], true),
                new NameValueModel<bool>(Lang["否"], false)
            };

            _baseUserAppService = baseUserAppService;
            _baseMenuAppService = baseMenuAppService;

            if (baseUser != null)
            {
                BaseUser = baseUser;
                IsAdd = false;
            }
        }

        protected override async Task LoadCommand_Executing(object parameter)
        {
            IReadOnlyList<BaseUserPermissionDto> permissions = new List<BaseUserPermissionDto>();
            if (!IsAdd)
            {
                permissions = await _baseUserAppService.GetPermissions(BaseUser.Account);
            }
            List<BaseMenuResultDto> baseMenuResults = await _baseMenuAppService.GetTopList();
            for (int i = 1; i < baseMenuResults.Count; i++)
            {
                BaseMenuResultDto item = baseMenuResults[i];
                Menus.Add(new NameValueSelectedModel<string>(Lang[item.DisplayName], item.Code, permissions.FirstOrDefault(p => p.Code.Equals(item.Code)) != null));
            }
        }

        protected override bool OkCommand_BeforeCanExecute(object parameter)
        {
            return BaseUser.IsValidated;
        }

        protected override bool OkCommand_BeforeExecute(object parameter)
        {
            if (BaseUser.Account.Equals(Constant.ADMIN) && BaseUser.IsActive == false)
            {
                this.ShowWarning("不能禁用超级管理员");
                return false;
            }
            return true;
        }

        protected override async Task<IEntityDto> OkAsync(object parameter)
        {
            BaseUser.Error = string.Empty;
            List<BaseUserPermissionDto> permissions = new List<BaseUserPermissionDto>();
            for (int i = 0; i < Menus.Count; i++)
            {
                if (!Menus[i].IsSelected)
                {
                    continue;
                }
                permissions.Add(new BaseUserPermissionDto
                {
                    Account = BaseUser.Account,
                    Code = Menus[i].Value,
                    Sort = (byte)(i + 1)
                });
            }
            BaseUserResultDto baseUser = await _baseUserAppService.Save(BaseUser, permissions);
            DataCache.UpdateLoginUser(baseUser);
            RaiseRequestClose();
            return baseUser;
        }

        protected override void OkCommand_UnexecutedFriendlyException(FriendlyException ex)
        {
            BaseUser.Error = ex.Message;
        }
    }
}
